<template>
  <div class="announcement-management-container">
    <el-card>
      <div slot="header">
        <span>公告管理</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleAdd">新增公告</el-button>
      </div>
      
      <el-table
        v-loading="loading"
        :data="announcements"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题">
        </el-table-column>
        <el-table-column
          label="内容"
          width="400">
          <template slot-scope="scope">
            <div class="content-preview">{{ scope.row.content }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="180">
          <template slot-scope="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px">
      <el-form :model="announcementForm" :rules="announcementRules" ref="announcementForm" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="announcementForm.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="announcementForm.content" rows="6"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAnnouncement">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAnnouncements, createAnnouncement, updateAnnouncement, deleteAnnouncement } from '@/api/announcement'
import { formatDate } from '@/utils/date'

export default {
  name: 'AnnouncementManagement',
  data() {
    return {
      loading: false,
      announcements: [],
      dialogVisible: false,
      dialogTitle: '新增公告',
      announcementForm: {
        id: null,
        title: '',
        content: ''
      },
      announcementRules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { max: 100, message: '标题长度不能超过100个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchAnnouncements()
  },
  methods: {
    formatDate,
    async fetchAnnouncements() {
      this.loading = true
      try {
        const res = await getAnnouncements()
        if (res.code === 200) {
          this.announcements = res.data
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        this.$message.error('获取公告列表失败')
      } finally {
        this.loading = false
      }
    },
    handleAdd() {
      this.dialogTitle = '新增公告'
      this.announcementForm = {
        id: null,
        title: '',
        content: ''
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑公告'
      this.announcementForm = {
        id: row.id,
        title: row.title,
        content: row.content
      }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该公告?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          const res = await deleteAnnouncement(row.id)
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.fetchAnnouncements()
          } else {
            this.$message.error(res.message)
          }
        } catch (error) {
          this.$message.error('删除公告失败')
        }
      }).catch(() => {})
    },
    submitAnnouncement() {
      this.$refs.announcementForm.validate(async valid => {
        if (valid) {
          try {
            let res
            if (this.announcementForm.id) {
              res = await updateAnnouncement(this.announcementForm)
            } else {
              res = await createAnnouncement(this.announcementForm)
            }
            
            if (res.code === 200) {
              this.$message.success(this.announcementForm.id ? '更新成功' : '新增成功')
              this.dialogVisible = false
              this.fetchAnnouncements()
            } else {
              this.$message.error(res.message)
            }
          } catch (error) {
            this.$message.error(this.announcementForm.id ? '更新公告失败' : '新增公告失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.announcement-management-container {
  padding: 20px;
}
.content-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style> 